package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.mapper.DishMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 海关监管和对方
* @description 针对表【dish(鑿滃搧绠＄悊)】的数据库操作Service实现
* @createDate 2024-05-06 10:13:07
*/
@Service

public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishMapper dishMapper;

    //因为在categoryServiceImpl中封装了DishServiceImpl，所以不能循环字段
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional//因为涉及多张表
    //添加
    public void saveWithFlavor(DishDto dishDto) {
        //dishDto继承自Dish，可以直接加入，框架智能到只会将父类的属性加入
        this.save(dishDto);

        //获取dishFlavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        //还有dishId没有赋值
        for (DishFlavor dishFlavor :flavors) {
            dishFlavor.setDishId(dishDto.getId());
        }
        //批量添加
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    //弃用的分页
    public R<Page<Dish>> pageR(Page<Dish> dishPage, LambdaQueryWrapper<Dish> wrapper) {
        dishMapper.selectPage(dishPage,wrapper);
        return R.success(dishPage);
    }

    /**
     * 分页时显示categoryName
     * @param dishPage
     * @param wrapper
     * @return
     */
    @Override
    public R showDishDto(Page<Dish> dishPage, LambdaQueryWrapper<Dish> wrapper) {
        //将查询结果Dish封装进dishPage
        dishMapper.selectPage(dishPage,wrapper);

        //最终要返回的是R<Page<DishDto>>
        //所以现在要封装出一个Page<DishDto>

        //1：封装List<DishDto>
        //1.1 取出List<Dish>
        List<Dish> dishList = dishPage.getRecords();

        //1.2改造List<Dish>  --->  List<DishDto>
        List<DishDto> dishDtoList = new ArrayList<DishDto>();
        for (Dish dish : dishList) {
            //封装dto
            DishDto dishDto = new DishDto();
            //1.2.1 拷贝原有的值
            //为什么一开始不这样写，因为觉得一个一个赋值很麻烦，看了教程，学会了拷贝，就简单了
            BeanUtils.copyProperties(dish,dishDto);
            //1.2.2 给categoryName赋值
            Long categoryId = dish.getCategoryId();
            String categoryName = categoryMapper.selectById(categoryId).getName();
            dishDto.setCategoryName(categoryName);
            dishDtoList.add(dishDto);
        }

        //2：封装dishDtoPage
        //2.1
        Page<DishDto> dishDtoPage = new Page<>();
        //2.2对象拷贝                                   除了records外的属性都拷贝
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //2.3放入List<DishDto>
        dishDtoPage.setRecords(dishDtoList);


        return R.success(dishDtoPage);
    }

    @Override
    //页面回显，需要dish和dish_flavor两张表
    public DishDto getMess(long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        List<DishFlavor> dishFlavors =
                dishFlavorMapper
                        .selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    @Transactional
    //修改
    public void updateWithFlavro(DishDto dishDto) {
        //1.更新dish表
        this.updateById(dishDto);

        //2.更新dish_flavor表

        //2.1:先将该菜品的所有口味删除
        //因为口味的数量也有可能发生改变
        dishFlavorMapper.
                delete(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,dishDto.getId()));
        //2.2
        //获取dishFlavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        //还有dishId没有赋值
        for (DishFlavor dishFlavor :flavors) {
            dishFlavor.setDishId(dishDto.getId());
        }
        //批量添加
        dishFlavorService.saveBatch(flavors);
    }
}




